package com.xiaohong.order.controller;

import com.xiaohong.order.interceptor.TokenRequestInterceptor;
import com.xiaohong.order.pojo.OrderItem;
import com.xiaohong.order.service.CartService;
import entity.Result;
import entity.StatusCode;
import entity.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
@CrossOrigin
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据id删除购物车商品
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteById(String id, HttpServletRequest httpServletRequest) {

        //获取token
        String token = httpServletRequest.getHeader("Authorization");
        TokenRequestInterceptor.token = token;

        //获取用户令牌信息
        String username = (String) TokenDecode.getUserInfo().get("username");

        //判断购物车是否有数据
        if (redisTemplate.boundHashOps("Cart_"+username).size() == 0 ||redisTemplate.boundHashOps("Cart_"+username).get(id) == null){
            return new Result(false, StatusCode.ERROR,"无效操作！");
        }

        cartService.deleteById(id,username);
        return new Result(true, StatusCode.OK,"根据id清理购物车成功！");

    }
    /**
     * 首次添加购物车
     * @param id
     * @return
     */
    @PostMapping("/add/{id}")
    public Result addFirst(@PathVariable(value = "id") String id, HttpServletRequest httpServletRequest){

        //获取token
        String token = httpServletRequest.getHeader("Authorization");
        TokenRequestInterceptor.token = token;

        //获取用户令牌信息
        String username = (String) TokenDecode.getUserInfo().get("username");

        //判断购物车是否有数据
        if (redisTemplate.boundHashOps("Cart_"+username).get(id) != null){
            return new Result(false, StatusCode.ERROR,"重复添加！");
        }
        cartService.addFirst(id,username);
        return new Result(true, StatusCode.OK,"添加购物车成功！");
    }

    /**
     * 更新购物车
     * @param id
     * @param num
     * @return
     */
    @PutMapping("/update")
    public Result update(String id, Integer num, HttpServletRequest httpServletRequest){
        //获取token
        String token = httpServletRequest.getHeader("Authorization");
        TokenRequestInterceptor.token = token;
        //获取用户令牌信息
        String username = (String) TokenDecode.getUserInfo().get("username");

        //判断购物车是否有数据
        if (redisTemplate.boundHashOps("Cart_"+username).size() == 0 || redisTemplate.boundHashOps("Cart_"+username).get(id) == null){
            return new Result(false, StatusCode.ERROR,"请先添加购物车！");
        }

        cartService.update(id,num,username);

        return new Result(true, StatusCode.OK,"更新购物车成功！",this.list());
    }

    /**
     * 获取购物车列表
     * @return
     */
    @GetMapping("/list")
    public Result<List<OrderItem>> list(){
        Map<String, Object> userInfo = TokenDecode.getUserInfo();
        System.out.println(userInfo);
        String username = (String) userInfo.get("username");
        List<OrderItem> orderItems = cartService.list(username);
        return new Result(true, StatusCode.OK,"查询购物车成功！",orderItems);
    }

}
